package user

import (
	"enjoy-shop/api/base"
	"enjoy-shop/formdata"
	"enjoy-shop/server/user"
	"fmt"
	"gitee.com/xuxinzhi2206/corehub/database/redis"
	"github.com/gin-gonic/gin"
	"github.com/go-pay/gopay/pkg/jwt"
	"net/http"
	"strconv"
)

// 用户登录
func Login(c *gin.Context) {
	var r formdata.Login
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	//验证手机号是否合法
	res := user.CheckPhone(r.Phone)
	if !res {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "手机号不合法", "data": nil})
		return
	}
	if r.Type == 1 || r.Type == 2 {
	} else {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "登录类型不能为空", "data": nil})
		return
	}
	info, err := user.Find(&r)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
	}
	// 判断账号是否存在
	if info.UserFirst.Phone == "" {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "账号不存在", "data": nil})
		return
	}
	//申明获取token
	jwtToken := user.Claims{
		Id:             "",
		StandardClaims: jwt.StandardClaims{},
	}
	//type = 1  密码登录
	if r.Type == 1 {
		if r.Pwd == "" {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": "密码不能为空", "data": nil})
			return
		}
		//验证密码是否正确
		encryptionPwd := user.PasswordEncryption(r.Pwd)
		if encryptionPwd != info.UserFirst.Pwd {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": "密码不正确", "data": nil})
			return
		}
		token, err := jwtToken.GenerateJWT(info.UserFirst.Id)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
			return
		}
		data := make(map[string]any)
		data["token"] = token
		data["info"] = map[string]any{
			"user_name": info.UserFirst.UserName,
			"img":       info.UserFirst.Img,
			"phone":     info.UserFirst.Phone,
			"sex":       info.UserFirst.Sex,
			"birthday":  info.UserFirst.Birthday,
		}
		//用户id存储到上下文中
		c.Set("user_id", info.UserFirst.Id)
		aa, _ := c.Get("user_id")
		fmt.Println("请求头", aa)
		c.JSON(http.StatusOK, gin.H{"code": 1, "message": "登录成功", "data": data})
		return
	}
	//type=2  验证码登录
	if r.Type == 2 {
		if r.Code == 0 {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": "验证码不能为空", "data": nil})
			return
		}
		val, err := redis.GetRedis(r.Phone + strconv.Itoa(int(r.Type)))
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
			return
		}
		//对比验证码是否正确
		if val.Val() != strconv.Itoa(int(r.Code)) {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": "验证码不正确", "data": nil})
			return
		}
		token, err := jwtToken.GenerateJWT(info.UserFirst.Id)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
			return
		}
		data := make(map[string]any)
		data["token"] = token
		data["info"] = map[string]any{
			"user_name": info.UserFirst.UserName,
			"img":       info.UserFirst.Img,
			"phone":     info.UserFirst.Phone,
			"sex":       info.UserFirst.Sex,
			"birthday":  info.UserFirst.Birthday,
		}
		//用户id存储到上下文中
		c.Set("user_id", info.UserFirst.Id)
		c.JSON(http.StatusOK, gin.H{"code": 1, "message": "登录成功", "data": data})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": -1, "message": "登录失败", "data": nil})
	return

}

// 发送验证码
func SendCode(c *gin.Context) {
	var r formdata.SendCode
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	//验证手机号是否合法
	res := user.CheckPhone(r.Phone)
	if !res {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "手机号不合法", "data": nil})
		return
	}
	if r.Type == "" {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "类型不能为空", "data": nil})
		return
	}
	//限制一分钟只能发送一次
	val, err := redis.GetRedis(r.Phone + r.Type + "minute")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}
	fmt.Println(val.Val())
	if val.Val() != "" {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "一分钟只能发送一次", "data": nil})
		return
	}
	_, err = user.SendCode(&r)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 1, "message": "验证码发送成功,10分钟内有效", "data": nil})
	return
}

// 用户注册
func UserRegister(c *gin.Context) {
	var r formdata.UserRegister
	var f formdata.Login
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	//验证手机号是否合法
	res := user.CheckPhone(r.Phone)
	if !res {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "手机号不合法", "data": nil})
		return
	}
	if r.Code == "" || r.Pwd == "" {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "验证码或者密码不能为空", "data": nil})
		return
	}
	f.Phone = r.Phone
	info, err := user.Find(&f)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}
	if info.UserFirst.Phone != "" {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "手机号已存在", "data": nil})
		return
	}
	//获取验证码
	val, err := redis.GetRedis(r.Phone + r.Type)
	if val.Val() != r.Code {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "验证码不正确", "data": nil})
		return
	}
	//验证密码是否正确
	r.Pwd = user.PasswordEncryption(r.Pwd)
	_, err = user.UserSave(&r)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 1, "message": "注册成功", "data": nil})
	return
}

// 用户修改
func Update(c *gin.Context) {
	var r formdata.UserUpdate
	var l formdata.Login
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}
	authorization := c.GetHeader("Authorization")
	claims := user.Claims{}
	data, err := claims.ValidateJWT(authorization)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}
	r.Id, _ = strconv.Atoi(data.Id)
	if r.Pwd != "" {
		r.Pwd = user.PasswordEncryption(r.Pwd)
	}
	if r.Phone != "" {
		l.Phone = r.Phone
		info, _ := user.Find(&l)
		if info != nil && info.UserFirst != nil && info.UserFirst.Phone != "" {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": "失败:更新的手机号已存在,请注销后再试", "data": nil})
			return
		}
	}
	_, err = user.UserUpdate(r)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 1, "message": "修改成功", "data": nil})
	return
}

// 检查验证码
func CheckCode(c *gin.Context) {
	var r formdata.CheckCode
	if err := c.ShouldBindJSON(&r); err != nil {
		base.RespData(c, nil, err)
		return
	}

	val, err := redis.GetRedis(r.Phone + r.Type)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": err, "data": nil})
		return
	}
	if val.Val() != strconv.Itoa(int(r.Code)) {
		c.JSON(http.StatusOK, gin.H{"code": -1, "message": "验证码不正确", "data": nil})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 1, "message": "验证码正确", "data": nil})
	return
}

// 中间件
// 定义中间
func MiddleWare() gin.HandlerFunc {
	return func(c *gin.Context) {
		authorization := c.GetHeader("Authorization")
		claims := user.Claims{}
		data, err := claims.ValidateJWT(authorization)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": -1, "message": err.Error(), "data": nil})
			return
		}
		c.Set("user_id", data.Id)
		c.Next()

	}
}
